package com.asiainfo.complier.ui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

import com.asiainfo.complier.common.ComplierInfo;
import com.asiainfo.complier.conf.SysSetting;
import com.asiainfo.complier.editor.CoderEditor;
import com.asiainfo.complier.helper.UIHelper;

public class ErrorPane extends JPanel {
	/**
	 * ErrorPane
	 */
	private static final long serialVersionUID = -827377791399384392L;

	private int rowHeight = 10;
	private ArrayList<ComplierInfo> complierInfo;
	private CoderEditor editor;
	private int curLine;
	private JWindow lineWindow = new JWindow(SysSetting.MAIN_Frame);
	private JLabel lbLine;

	public ErrorPane(final CoderEditor editor, final boolean showLine) {
		setLayout(null);
		lbLine = new JLabel();
		lbLine.setPreferredSize(new Dimension(100, 13));
		lineWindow.setSize(new Dimension(102, 15));
		lineWindow.add(lbLine);
		lineWindow.setBackground(Color.white);
		lbLine.setBackground(Color.WHITE);
		lbLine.setBorder(BorderFactory.createLineBorder(Color.black));
		this.editor = editor;

		this.addMouseMotionListener(new MouseMotionListener() {

			public void mouseMoved(MouseEvent e) {
				if (showLine) {
					curLine = e.getY() * editor.getEditor().getLineCount()
							/ getHeight();
					if (curLine <= 0)
						return;
					int x = 0;
					int y = e.getY();
					Point point = new Point(x, y);
					SwingUtilities.convertPointToScreen(point, ErrorPane.this);
					lbLine.setText("Line: " + curLine);
					lineWindow.setLocation(
							point.x - lineWindow.getWidth() - 18, point.y);

				}
			}

			public void mouseDragged(MouseEvent e) {

			}
		});
		this.addMouseListener(new MouseAdapter() {

			public void mouseExited(MouseEvent e) {
				if (showLine) {
					lineWindow.setVisible(false);
				}

			}

			public void mouseEntered(MouseEvent e) {

				if (showLine) {

					curLine = e.getX() * editor.getEditor().getLineCount()
							/ getHeight();

					lineWindow.setVisible(true);
				}

			}

			public void mouseClicked(MouseEvent e) {
				if (showLine && curLine > 0) {
					editor.go2Line(curLine);
				}
			}
		});

	}

	/**
	 * UpdateError
	 * 
	 * @param startLine
	 * @param endLine
	 */
	public void UpdateError(int startLine, int endLine) {
		this.removeAll();
		this.setLayout(null);
		if (complierInfo != null) {

			int i = 1;
			for (final ComplierInfo info : complierInfo) {

				int x = 2;
				int y = rowHeight * i;
				JButton eButton = new JButton(" ");
				eButton.setBorder(new EmptyBorder(1, 1, 1, 1));

				eButton.setBackground(Color.red);
				eButton.setCursor(Cursor
						.getPredefinedCursor(Cursor.HAND_CURSOR));
				eButton.addActionListener(new ActionListener() {

					public void actionPerformed(ActionEvent paramActionEvent) {
						editor.go2Line(info.getErrorLine());
					}
				});
				eButton.setToolTipText("line: " + info.getErrorLine() + " "
						+ info.getErrorMessage());
				eButton.setBounds(x, y - 6, 18, 6);
				this.add(eButton);
				i++;
			}

		}
		this.updateUI();

	}

	/**
	 * 
	 * @param startLine
	 * @param endLine
	 */
	public void UpdateImageError(int startLine, int endLine) {
		this.removeAll();
		this.setLayout(null);
		ImageIcon errImage = UIHelper
				.getImageIco("com/asiainfo/images/error.png");
		if (complierInfo != null) {

			ArrayList<ComplierInfo> curList = new ArrayList<ComplierInfo>();
			for (ComplierInfo info : complierInfo) {
				if (info.getErrorLine() >= startLine
						&& info.getErrorLine() <= endLine) {
					curList.add(info);
				}
			}

			for (final ComplierInfo info : curList) {
				int x = 0;
				JLabel eButton = new JLabel();
				int y = editor.getEditor().getRowHeight()
						* (info.getErrorLine() - startLine + 1);
				eButton.setBackground(Color.red);
				eButton.setCursor(Cursor
						.getPredefinedCursor(Cursor.HAND_CURSOR));

				eButton.setToolTipText("line: " + info.getErrorLine() + " "
						+ info.getErrorMessage());
				eButton.setIcon(errImage);
				eButton.setBounds(x, y - errImage.getIconHeight(), 16, 16);
				eButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						editor.getEditor().go2Line(info.getErrorLine());

					}
				});

				this.add(eButton);
			}
		}

		this.updateUI();

	}

	public void setComplierInfo(ArrayList<ComplierInfo> complierInfo) {
		this.complierInfo = complierInfo;
	}

	public ArrayList<ComplierInfo> getComplierInfo() {
		return complierInfo;
	}

	public void setRowHeight(int rowHeight) {
		this.rowHeight = rowHeight;
	}

	public int getRowHeight() {
		return rowHeight;
	}

}
